package longrise.phone.com.bjjt_henan.Utils;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.view.View;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.widget.HeNanDialog;

/**
 * Created by Administrator on 2016/7/16.
 */
public class BDLocationManager implements MainActivity.OnActivityReturnBitmap {

    private Context context;

    private HeNanDialog dialog;

    private LocationClient client;

    private BDLocationListener listener;

    public BDLocationManager(Context context) {
        this.context = context;
        if (context instanceof MainActivity) {
            ((MainActivity) context).setOnActivityReturnBitmap(this);
        }
    }

    //开始定位获取经纬度
    public void startLocation() {
        if (checkGPS()) {
            bdLocation();
        }
    }

    //检测GPS功能是否开启
    private boolean checkGPS() {
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            showGPSDialog();
            return false;
        }
        return true;
    }

    //显示提示对话框
    private void showGPSDialog() {
        if (dialog == null) {
            dialog = UiUtils.showDialog(context, "GPS定位功能未开启,是否打开？", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            }, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    ((MainActivity) context).startActivityForResult(intent, 66);
                }
            });
        } else {
            dialog.show();
        }
    }

    //启动百度定位获取经纬度
    private void bdLocation() {
        if (client == null) {
            LocationClientOption option = new LocationClientOption();
            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
//            option.setCoorType("gcj02");// 可选，默认gcj02，设置返回的定位结果坐标系，
            option.setCoorType("bd09ll");
            option.setScanSpan(3000);// 可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
            option.setIsNeedAddress(true);// 可选，设置是否需要地址信息，默认不需要
            option.setOpenGps(true);// 可选，默认false,设置是否使用gps
            option.setLocationNotify(true);// 可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
            option.setIgnoreKillProcess(true);// 可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
            // 开始定位
            client = new LocationClient(context, option);
            client.registerLocationListener(listener);
            if (null != client && !client.isStarted()) {
                client.start();
            }
        }
    }

    @Override
    public void activityReturnBitmap(int requestCode, int resultCode, Intent data) {
        if (requestCode == 66) {
            bdLocation();
        }
    }

    public void ondestroty() {
        if (client != null) {
            client.stop();
            client = null;
        }
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public void setLocationListener(BDLocationListener listener) {
        this.listener = listener;
    }
}
